package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Warehouse;
import com.blb.service.IWarehouseService;
import com.blb.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *仓库管理的仓库设置
 * @author wss
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private IWarehouseService warehouseService;

    /**
     * 仓库分页列表
     * @param cTime
     * @param draw
     * @param wName
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("storyshezhi")
    @RequestMapping("warehouse_manage")
    public DataTablesPage<Warehouse> list(String cTime,Integer draw, String wName, Integer start, Integer length) {
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isEmpty(cTime)){
            map.put("cTime",null);
        }else {
            map.put("cTime",'%'+cTime+"%");
        }
        String companyId = ShiroUtils.getCompany();
        map.put("companyId",companyId);
        map.put("wName",wName);
        int pageCount = warehouseService.getPageCount(map);
        map.put("start",start);
        map.put("length",length);
        List<Warehouse> pageList = warehouseService.getPageList(map);
        return new DataTablesPage<>(draw,pageCount,pageCount,pageList) ;
    }

    /**
     * 添加仓库信息
     * @param warehouse
     * @return
     */
    @RequiresPermissions("storyshezhi:add")
    @PostMapping("add")
    public Response add(Warehouse warehouse) {
        warehouse.setId(UUIDUtils.getId());
        warehouse.setCompanyId(ShiroUtils.getCompany());
        warehouse.setCreateTime(TimeUtils.getNowTime());
        warehouseService.save(warehouse);
        return Response.success("添加成功");
    }

    /**
     * 修改仓库信息
     * @param warehouse
     * @return
     */
    @RequiresPermissions("storyshezhi:update")
    @PostMapping("update")
    public Response update(Warehouse warehouse) {
        warehouseService.updateById(warehouse);
        return Response.success();
    }

    /***
     * 修改仓库状态
     * @param warehouse
     * @return
     */
    @RequiresPermissions("storyshezhi:update")
    @PostMapping("updateStatus")
    public Response updateStatus(Warehouse warehouse) {
        Warehouse pojo= warehouseService.getById(warehouse.getId());
        pojo.setWarehouseStatus(warehouse.getWarehouseStatus());
        warehouseService.updateById(pojo);
        return Response.success();
    }

    /***
     * 根据公司id过滤仓库信息
     * @return
     */
    @RequiresPermissions("storyshezhi")
    @PostMapping("list")
    public List<Warehouse> list()
    {  String companyId = ShiroUtils.getCompany();
        return warehouseService.list(new QueryWrapper<Warehouse>().eq("company_id",companyId));
    }

    /***
     * 左侧货架树形图
     * @return
     */
    @RequiresPermissions("storyshezhi")
    @RequestMapping("listTree")
    public List<Warehouse> listTree() {
        Map<String,Object> map = new HashMap<>();
        String companyId = ShiroUtils.getCompany();
        map.put("companyId",companyId);
        List<Warehouse> list = warehouseService.listTree(map);
        list.forEach(warehouse -> {
            warehouse.setName(warehouse.getWarehouseNo() + "-" + warehouse.getWarehouseName());
            warehouse.getChildren().forEach(area -> area.setName(area.getAreaNo() + "-" + area.getAreaName()));
        });
        return list;
    }

    /**
     * 库存查询分页列表
     * @param wName
     * @param wId
     * @param pid
     * @param pName
     * @param draw
     * @param start
     * @param length
     * @return
     */
    @RequiresPermissions("storynum")
    @RequestMapping("inventory")
    DataTablesPage<Warehouse> inventory(String wName,String wId,String pid,String pName,Integer draw, Integer start, Integer length) {
        Map<String,Object> map = new HashMap<>();
        String companyId = ShiroUtils.getCompany();
        map.put("companyId",companyId);
        map.put("wName",wName);
        map.put("wId",wId);
        map.put("pid",pid);
        if (StringUtils.isEmpty(pName)){
            map.put("pName",null);
        }else {
            map.put("pName","%"+pName+"%");
        }
        int pageCount = warehouseService.getPageCountByInventory(map);
        map.put("start",start);
        map.put("length",length);
        List<Warehouse> pageList = warehouseService.getProductByWarehouse(map);
        return new DataTablesPage<>(draw,pageCount,pageCount,pageList) ;
    }

}

